package xzr4;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class jisuanqi {
    String n1;
    String n2;
    String operate;
    String num = "";
    double result;
    JFrame jf; //计算机界面的窗体
    JTextField jtf; //文本域输入框
    JButton[] jbs; //存放16个按钮组件的数组
    private JButton clear; //清空按钮组件


    public jisuanqi() {
        jf = new JFrame("计算器v1.1"); //初始化窗体组件
        jtf = new JTextField(15);
        clear = new JButton("清空");
        clear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == clear){
                    jtf.setText("");
                    n1 = null;
                    n2 = null;
                    num = "";
                }
            }
        });
        jbs = new JButton[16];
        String str = "123+456-789*0.=/";
        for (int i = 0; i < jbs.length; i++) {
            jbs[i] = new JButton(str.charAt(i)+"");
            jbs[i].addActionListener(new ButClick());
        }
        init();
        jf.setSize(500,500);
        jf.setVisible(true);
    }
    private void init(){
        jf.setLayout(new BorderLayout());
        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        jp1.setLayout(new FlowLayout());
        jp1.add(jtf);
        jp1.add(clear);
        jp2.setLayout(new GridLayout(4,4));
        for (int i = 0; i < jbs.length; i++) {
            jp2.add(jbs[i]);
        }
        jf.add(jp1,BorderLayout.NORTH);
        jf.add(jp2,BorderLayout.CENTER);
    }
    class ButClick implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            String text = ((JButton)(e.getSource())).getText();
            if(text.equals("=")){
                if(operate.equals("+")){
                    result = Double.parseDouble(n1)+Double.parseDouble(n2);
                    jtf.setText(result+"");
                }else if(operate.equals("-")){
                    result = Double.parseDouble(n1)-Double.parseDouble(n2);
                    jtf.setText(result+"");
                }else if(operate.equals("*")){
                    result = Double.parseDouble(n1)*Double.parseDouble(n2);
                    jtf.setText(result+"");
                }else if(operate.equals("/")){
                    result = Double.parseDouble(n1)/Double.parseDouble(n2);
                    jtf.setText(result+"");
                }
                n1 = null;
                n2 = null;
                num = "";
                operate = null;
            }else if(text.equals("+")){
                operate="+";
                num+=text;
                jtf.setText(num);
            }else if(text.equals("-")){
                operate="+";
                num+=text;
                jtf.setText(num);
            }else if(text.equals("*")){
                operate="*";
                num+=text;
                jtf.setText(num);
            }else if(text.equals("/")){
                operate="/";
                num+=text;
                jtf.setText(num);
            }else if(n1!=null&&operate==null){
               n1+=text;
               num+=text;
               jtf.setText(num);
            }else if(n2!=null&&operate==null){
                n2+=text;
                num+=text;
                jtf.setText(num);
            }else {
                if(n1 == null){
                    n1 = text;
                }else {
                    n2 = text;
                }
                num+=text;
                jtf.setText(num);
            }
        }
    }

    public static void main(String[] args) {
    jisuanqi jisuanqi1 = new jisuanqi();


    }
}
